package com.wantup.springboot.boot.jms.mkw.jms_activemq.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2018/11/15 16:24
 *
 * 消费者 用来 消费消息的
 * 主题模式下的
 */
public class AppCustomer {

    private static final String url = "tcp://47.94.142.36:61616";
    private static final String topicName = "topic-test";


    public static void main(String[] args) throws JMSException {
        //1
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        //2
        Connection connection = connectionFactory.createConnection();
        //3
        connection.start();
        //4
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5
        Destination destination = session.createTopic(topicName);
        //6 创建消费者
        MessageConsumer consumer = session.createConsumer(destination);

        //7. 创建监听器
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接收到的 消息： -->  "+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });


    }


}
